<?php
  define('ACC', true);
require('./include/init.php');

session_start(); 
//print_r($_SESSION);
$userid = $_SESSION['user_id'];

echo '<head><meta charset = "utf-8"></head>';  
//$userid = 26;
 echo '<h2>采购&nbsp&nbsp<a href = "dashboard.php">返回虚拟公司主页</a></h2><br/>';   

$company = new companymodel();
$companyinfo = $company->getcompanyinfo($userid);
$companyid = $companyinfo['company_id'];

echo '<h2>公司名称为:' . $companyinfo['company_name'], '&nbsp|&nbsp';
echo '目前所在财年:第' . $companyinfo['company_y'] . '财年', '&nbsp|&nbsp';
echo '目前所在季度:第' . $companyinfo['company_q'] . '季度', '</h2><br/><br/><br/>';

$purchaseinfo = $_POST;
$msg='';


switch($purchaseinfo['type']){
    case 'building':
    $elementlist = $company->getelementlist('building');
    foreach($elementlist as $k=>$v){
        $elementname = $v['element_name'];
        $elementid = $v['element_id'];
        if($purchaseinfo[$elementname]!=''){
            $succeed = $company->actionpurchasebuilding($companyid, $elementid, $purchaseinfo[$elementname]); 
            if($succeed != false){
                $msg = $msg . '采购' .$elementname . '，数量：' .  $purchaseinfo[$elementname] . '->成功！<br/>';
            }else{
                $msg = $msg . '采购' .$elementname . '，数量：' .  $purchaseinfo[$elementname] . '->失败！<br/>';
            }
        } 
    }
    break;
    
    case 'equipment':
    $succeed = $company->actionpurchaseequipment($purchaseinfo['building_tagid'], $purchaseinfo['position'], $purchaseinfo['element_id']);
    if($succeed != false){
        $msg = $msg . '采购成功！<br/>';
    }else{
        $msg = $msg . '采购失败！<br/>';
    }
    break;

 
    
    case 'raw_material':
    $elementlist = $company->getelementlist('raw_material');
    foreach($elementlist as $k=>$v){
        $elementname = $v['element_name'];
        $elementid = $v['element_id'];
        if($purchaseinfo[$elementname]!=''){
            $succeed = $company->actionpurchaserawmaterial($companyid, $elementid, $purchaseinfo[$elementname]); 
            if($succeed != false){
                $msg = $msg . '采购' .$elementname . '，数量：' .  $purchaseinfo[$elementname] . '->成功！<br/>';
            }else{
                $msg = $msg . '采购' .$elementname . '，数量：' .  $purchaseinfo[$elementname] . '->失败！<br/>';
            }
        } 
    }
    break; 

}
echo $msg;




?>
